package club.janna.netty.pipeline;


import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.util.concurrent.TimeUnit;

public class OutboundPipelineDemo {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup boosGroup = new NioEventLoopGroup(1);
        EventLoopGroup workGroup = new NioEventLoopGroup();

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(boosGroup, workGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new OutboundHandlerA());
                        pipeline.addLast(new OutboundHandlerB());
                        pipeline.addLast(new OutboundHandlerC());
                    }
                });
        ChannelFuture future = bootstrap.bind(8080).sync();
        System.out.println("启动完成，8080");
        future.channel().closeFuture().sync();

        boosGroup.shutdownGracefully();
        workGroup.shutdownGracefully();
    }

    private static class OutboundHandlerA extends ChannelOutboundHandlerAdapter {
        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            System.out.println("OutboundHandlerA added");
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("OutboundHandlerA write -> " + msg);
            ctx.write(msg);
        }
    }

    private static class OutboundHandlerB extends ChannelOutboundHandlerAdapter {
        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            System.out.println("OutboundHandlerB added");

            ctx.executor().schedule(() -> {
//                ctx.channel().write("Hello, world!");
                ctx.write("hello, world!");
//                ctx.pipeline().write("hello, world!");
            }, 3, TimeUnit.SECONDS);
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("OutboundHandlerB write -> " + msg);
            ctx.write(msg);
        }
    }

    private static class OutboundHandlerC extends ChannelOutboundHandlerAdapter {
        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            System.out.println("OutboundHandlerC added");
        }

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            System.out.println("OutboundHandlerC write -> " + msg);
            ctx.write(msg);
        }
    }
}
